/* === LICENSE INFO ===
 * This source file is subject to the GPLv3 license that is bundled with this
 * package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl.txt
 * === end of LICENSE INFO === */

/*==============================================================================
  File Name   : main.cpp
  Description : Test driver for GNetwork module of gUtil.

  Created on  : 2009-06-04
  Updated on  : 2010-06-20
  Updated by  : Shiou Ming, Lee

  Cautions    : (important notes)
  ============================================================================*/

//------------------------------------------------------------------------------
//-- INCLUDES & DEFINES
//------------------------------------------------------------------------------
#include "../GNetwork.hpp"
#include <iostream>
#include <stdexcept>


//------------------------------------------------------------------------------
//-- TYPEDEF / STRUCT / UNION / ENUM
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//-- GLOBAL
//------------------------------------------------------------------------------
const std::string g_AppName       = "Test driver";
const std::string g_AppVersion    = "1.0.0.7";
const std::string g_ModuleName    = "GNetwork module";
const std::string g_ModuleVersion = "1.0.0.7";


//------------------------------------------------------------------------------
//-- FUNCTION PROTOTYPES
//------------------------------------------------------------------------------


//------------------------------------------------------------------------------
//-- MAIN FUNCTION
//------------------------------------------------------------------------------
int main(int _argc, char* _argv[])
{
    std::cout << std::endl << "======" << std::endl;
    std::cout << g_AppName.c_str() << ", version " << g_AppVersion.c_str();
    std::cout << std::endl << "for " << g_ModuleName.c_str() <<
        ", version " << g_ModuleVersion.c_str();
    std::cout << std::endl << "======" << std::endl << std::endl;

    try
    {
        TCPClientSocket *socket1 = NULL;
        TCPClientSocket *socket2 = NULL;
        TCPClientSocket *socket3 = NULL;

        // Create TCP client socket with specific remote address
        socket1 = new TCPClientSocket("127.0.0.1", 9999);

        // Create TCP client socket, but set remote address later on
        socket2 = new TCPClientSocket();
        socket2->setRemoteAddress("127.0.0.1", 9998);

        // Connect to a TCP server
        socket1->connectServer();
        socket2->connectServer();
        socket3 = new TCPClientSocket();
        socket3->connectServer("127.0.0.1", 9997);

        // Send text data to connected TCP server
        socket1->sendTextData("Test data... muahahah");

        // Attempt to receive any text data from TCP server
        char dataBuffer[0];
        memset(dataBuffer, 0, sizeof(dataBuffer));
        socket1->receiveTextData(dataBuffer, sizeof(dataBuffer));

        // Cleanup
        if (NULL != socket1)
        {
            delete socket1;
            socket1 = NULL;
        }

        if (NULL != socket2)
        {
            delete socket2;
            socket2 = NULL;
        }

        if (NULL != socket3)
        {
            delete socket3;
            socket3 = NULL;
        }
    }
    catch (std::exception& e)
    {
        std::cerr << "[EMERGENCY] main(): " << e.what() << std::endl;
    }
    catch (...)
    {
        std::cerr << "[EMERGENCY] main(): Unknown exception detected." <<
                std::endl;
    }

    //-- Stop the console screen before quit for displaying the output.
    std::cout << std::endl << std::endl << "Press <ENTER> key to exit";
    std::cin.get();

    return 0;
}


//------------------------------------------------------------------------------
//-- FUNCTION IMPLEMENTATIONS
//------------------------------------------------------------------------------

